//
//  CollectButton.swift
//  XiaoXiangMu1604
//
//  Created by qianfeng on 16/9/29.
//  Copyright © 2016年 SunPiaoLiang. All rights reserved.
//

import UIKit

protocol CollectButtonDelegate:NSObjectProtocol{
    
    //删除的功能
    func didDeleteButtonAtIndex(index:Int)
    
}
class CollectButton: UIControl {

    weak var delegate:CollectButtonDelegate?
    
    private var imageView: UIImageView?
    
    private var titleLabel: UILabel?
    
    private var deleteBtn: UIButton?
    
    //按钮的序号
    var btnIndex: Int?
    
    //数据
    var model: CollectModel?{
        
        didSet {
            
            //显示数据
            //图片
            imageView?.image = model?.headImage
            
            //文字
            titleLabel?.text = model?.name
            
        }
        
    }
    //删除状态
    var isDelete: Bool?{
        
        didSet{
            
            if isDelete == true{
                
                deleteBtn?.hidden = false
                
            }else if isDelete == false{
                
                deleteBtn?.hidden = true
                
            }
            
        }
        
    }
    
    override init(frame: CGRect) {
        
        var tmpFrame = frame
        
        tmpFrame.size = CGSizeMake(100, 120)
        
        super.init(frame:tmpFrame)
        
        imageView = UIImageView(frame: CGRectMake(20, 20, 80, 80))
        
        addSubview(imageView!)
        
        titleLabel = UILabel.createLabelFrame(CGRectMake(20, 80, 80, 20), title:nil, textAlignment: .Center)
        
        addSubview(titleLabel!)
        
        deleteBtn = UIButton.createBtn(CGRectMake(0, 0, 40, 40), title: nil, bgImageName: "close", target: self, action: "deleteAction")
        
        addSubview(deleteBtn!)
        
        deleteBtn?.hidden = true
        
    }
    //删除操作
    func deleteAction(){
    
        
      delegate?.didDeleteButtonAtIndex(btnIndex!)
    
    
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
    
}
